package com.xinsoft.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysWarehouseItemParam;
import com.xinsoft.entity.po.SysWarehouse;
import com.xinsoft.entity.po.SysWarehouseItem;
import com.xinsoft.service.SysWarehouseItemService;
import com.xinsoft.service.SysWarehouseService;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName SysWareHouseController
 * @Description 仓位管理控制层
 * @Author lijie
 * @Date 2023-5-24 10:24:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/wareHouse/item")
@Api(value = "SysWareHouseItemController", tags = {"仓位管理控制层"})
public class SysWareHouseItemController {

	@Autowired
	private SysWarehouseItemService itemService;

	@Autowired
	private SysWarehouseService warehouseService;

	/**
	 * @Author Li9527
	 * @Description 仓位管理-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "仓位管理-新增", businessType = BusinessType.INSERT)
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody SysWarehouseItem sysWarehouseItem) {
		// 参数校验
		ValidatorUtils.validateEntity(sysWarehouseItem);
		// 默认已审核
		sysWarehouseItem.setAuditId(Const.ADMIN_UID);
		sysWarehouseItem.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
		SysWarehouseItem oldItem = itemService.getOne(new QueryWrapper<SysWarehouseItem>().eq("wh_item_name",sysWarehouseItem.getWhItemName()));
		if(oldItem != null){
			return Response.fail("该仓位名已存在,请不要重复添加");
		}
		// 构造仓位编号。取最后一个的id前面拼上CK00
		List<SysWarehouseItem> list = itemService.list(new QueryWrapper<SysWarehouseItem>().orderByDesc("id"));
		if(list.size() > 0 ){
			sysWarehouseItem.setWhItemCode(Const.WAREHOUSE_ITEM_CODE + list.get(0).getId());
		}else{
			sysWarehouseItem.setWhItemCode(Const.WAREHOUSE_ITEM_CODE + Const.NODE_DEFAULT_SORT);
		}
		return Response.succeed(itemService.save(sysWarehouseItem));
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@SysLog(title = "仓位管理-编辑", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody SysWarehouseItem sysWarehouseItem) {
		// 参数校验
		ValidatorUtils.validateEntity(sysWarehouseItem);
		// 参数校验
		SysWarehouseItem oldItem = itemService.getOne(new QueryWrapper<SysWarehouseItem>().eq("wh_item_name",sysWarehouseItem.getWhItemName()));
		if(oldItem != null && !sysWarehouseItem.getId().equals(oldItem.getId())){
			return Response.fail("该仓库名已存在,请不要重复添加");
		}
		return Response.succeed(itemService.updateById(sysWarehouseItem));
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		SysWarehouseItem warehouseItem = itemService.getById(id);
		// 注入仓库名称
		SysWarehouse warehouse = warehouseService.getOne(new QueryWrapper<SysWarehouse>().eq("id",warehouseItem.getWarehouseId()));
		if(warehouse != null){
			warehouseItem.setWarehouseName(warehouse.getWarehouseName());
		}
		return Response.succeed(warehouseItem);
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@SysLog(title = "仓位管理-删除", businessType = BusinessType.DELETE)
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		for (int i = 0; i < ids.size(); i++) {
			SysWarehouseItem item = itemService.getById(ids.get(i));
			if(item != null && item.getAuditStatus().equals(Const.AUDIT_STATUS_NORMAL)){
				return Response.fail("存在已审核仓位，不允许删除");
			}
		}
		itemService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 仓位管理分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody SysWarehouseItemParam param) {
		return Response.succeed(itemService.findPageListByParam(param));
	}

	/**
	 * @Author Li9527
	 * @Description 仓位管理不分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@GetMapping("/list")
	public Response<?> list(@RequestParam(value = "warehouseId", defaultValue = "", required = true) Integer warehouseId) {
		List<SysWarehouseItem> list = itemService.list(new QueryWrapper<SysWarehouseItem>().eq("warehouse_id",warehouseId));
		return Response.succeed(list);
	}


	/**
	 * @Author Li9527
	 * @Description 批量审核
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("仓位管理-批量审核")
	@SysLog(title = "仓位管理-批量审核", businessType = BusinessType.UPDATE)
	@PostMapping("/auditBatch")
	public Response<?> auditBacth(@RequestBody List<Integer> ids) {
		Integer auditId =  TokenUtil.getUser().getId();
		List<SysWarehouseItem> list = itemService.list(new QueryWrapper<SysWarehouseItem>().in("id",ids));
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setAuditStatus(Const.AUDIT_STATUS_NORMAL);
			list.get(i).setAuditId(auditId);
		}
		itemService.updateBatchById(list);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 批量反审核
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("仓位管理-批量反审核")
	@SysLog(title = "仓位管理-批量反审核", businessType = BusinessType.UPDATE)
	@PostMapping("/unAuditBatch")
	public Response<?> unAuditBatch(@RequestBody List<Integer> ids) {
		List<SysWarehouseItem> list = itemService.list(new QueryWrapper<SysWarehouseItem>().in("id",ids));
		Integer auditId =  TokenUtil.getUser().getId();
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setAuditStatus(Const.AUDIT_STATUS_WAIT);
			list.get(i).setAuditId(auditId);
		}
		itemService.updateBatchById(list);
		return Response.succeed();
	}


	/**
	 * 导入
	 */
	@PostMapping("/import")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
		return Response.succeed(itemService.importExcel(multipartFile));
	}

}
